package com.gexiaobao.pigeon.ui.home.activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.webkit.JsResult
import android.webkit.WebChromeClient
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.databinding.ActivityIndustrialRegisterBinding
import com.gexiaobao.pigeon.viewmodel.MainViewModel
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2025/2/5 11:48
 * @Describe    : 工商注册
 */
class ActivityIndustrialRegister : BaseActivity<MainViewModel, ActivityIndustrialRegisterBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)

        mDatabind.includeBar.tvTitle.text = "工商注册"

        initWebView()
    }

    @SuppressLint("JavascriptInterface")
    private fun initWebView() {
        val webSettings: WebSettings = mDatabind.webView.settings
        // 启用JavaScript
        webSettings.javaScriptEnabled = true
        // 启用支持缩放
        webSettings.builtInZoomControls = true
        webSettings.displayZoomControls = false // 隐藏默认的缩放按钮
        mDatabind.webView.webChromeClient = webChromeClient
        // 可以选择加载一个网页
        mDatabind.webView.loadUrl("https://gjzwfw.www.gov.cn/col/col1460/index.html")
//        mDatabind.webView.loadUrl("https://fp.aipgd.com/payment/views/payment.html?id=532e6ce944b048718b874f6eb4f408a1&sellerId=e954986766db485cb8b8c5a213f283a9")
        // 确保链接在WebView内打开
        mDatabind.webView.webViewClient = WebViewClient()
    }

    private val webChromeClient: WebChromeClient = object : WebChromeClient() {

        override fun onJsAlert(view: WebView?, url: String?, message: String?, result: JsResult?): Boolean {
            return super.onJsAlert(view, url, message, result)
        }

        override fun onReceivedTitle(view: WebView?, title: String?) {
            super.onReceivedTitle(view, title)
        }

        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            super.onProgressChanged(view, newProgress)
            mDatabind.progressBar.visibility = View.VISIBLE
            mDatabind.progressBar.progress = newProgress
            if (newProgress == 100) {
                mDatabind.progressBar.visibility = View.GONE
            }
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack) {
            when (it) {
                mDatabind.includeBar.ivBack -> finish()
            }
        }
    }

    override fun onBackPressed() {
        if (mDatabind.webView.canGoBack()) {
            mDatabind.webView.goBack()
        } else {
            super.onBackPressed()
        }
    }

    override fun onDestroy() {
        mDatabind.webView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null)
        mDatabind.webView.clearHistory()
        mDatabind.webView.destroy()
        mDatabind.webView.clearCache(true) // 清除缓存
        super.onDestroy()
    }
}